import { Button, Label, Node } from "cc";
import { IBlackboard } from "../../../../app-core/behaviorA/base/IBlackboard";
import { injectable } from "../../../../app-core/inject";
import { Mediator } from "../../../../app-core/mvcs/Mediator";
import LoginComp from "./component/LoginComp";

@injectable('LoginMediator')
export class LoginMediator extends Mediator implements IBlackboard {
    onRegister(): void {
        let rootNode: Node = this.getView();

        let startBtn = rootNode.getChildByPath('root/btn_hello');
        startBtn.on(Button.EventType.CLICK, this.onStartClickCallback, this);

        let addBtn = rootNode.getChildByPath('root/btn_add');
        addBtn.on(Button.EventType.CLICK, this.onAddClickCallback, this);

        let clearBtn = rootNode.getChildByPath('root/btn_clear');
        clearBtn.on(Button.EventType.CLICK, this.onClearClickCallback, this);
    }

    onRemove(): void {

    }

    onStartClickCallback(btn: Button) {
        this.dispatch('LoginCommand')
    }

    onAddClickCallback(btn: Button) {
        let loginComp: LoginComp = this.getView().getComponent(LoginComp);
        loginComp.addItem();
    }

    onClearClickCallback(btn: Button) {
        let loginComp: LoginComp = this.getView().getComponent(LoginComp);
        loginComp.clearItem();
    }

    login() {
        let rootNode: Node = this.getView();

        let labelTips = rootNode.getChildByPath('root/Label').getComponent(Label);
        labelTips.string = '你好，你已经登录！'
    }

    logout() {
        let rootNode: Node = this.getView();

        let labelTips = rootNode.getChildByPath('root/Label').getComponent(Label);
        labelTips.string = '你好，你已经退出！'
    }
}
